package com.ios.service.impl;

import com.ios.mapper.RoomMapper;
import com.ios.mapper.SeatMapper;
import com.ios.model.entity.Room;
import com.ios.model.entity.Seat;
import com.ios.service.RoomService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private SeatMapper seatMapper;

    @Override
    public AjaxResult insertRoom(Room room) {
        room.setCreatorId(SecurityUtils.getUserId());
        int rows = roomMapper.insertRoom(room);
        return rows > 0 ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    @Override
    public AjaxResult updateRoom(Room room) {
        int rows = roomMapper.updateRoom(room);
        return rows > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Override
    @Transactional
    public int deleteRoomByIds(Long[] ids) {
        // 先删除关联的座位
        for (Long id : ids) {
            seatMapper.deleteSeatByRoomId(id);
        }
        // 再删除自习室
        return roomMapper.deleteRoomByIds(ids);
    }

    @Override
    public Room selectRoomById(Long id) {
        return roomMapper.selectRoomById(id);
    }

    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    @Override
    public int countSeatsByRoomId(Long roomId) {
        return roomMapper.countSeatsByRoomId(roomId);
    }

    @Override
    public List<Seat> selectSeatsByRoomId(Long roomId) {
        return roomMapper.selectSeatsByRoomId(roomId);
    }
}